﻿package rob.core{
	import rob.core.Primitive3d;
	import rob.maths.*;
	import rob.data.*;
	
	public class Camera3d extends Primitive3d{
		private var _sz:Number;
		private var _up:Vect3d;
		
		public function Camera3d():void{
			_up = new Vect3d(0,0,-1);
		}
		
		public function lookAt(p:Vert3d):void{
			with(basis){
				k = VertexMaths.sub(position,p);
				VectorMaths.normalise(k);
				i = VectorMaths.cp(_up,k);
				j = VectorMaths.cp(k,i);
			}
		}
		
		public function rotateBy(dtheta:Number):void{
			var ix:Number, iz:Number;
			with(basis){
				ix = i.x;
				iz = i.z;
				i.x = Math.cos(dtheta)*ix - Math.sin(dtheta)*iz;
				i.z = Math.sin(dtheta)*ix + Math.cos(dtheta)*iz;
				j = VectorMaths.cp(k,i);
			}
		}
		
		public function moveRel(dx:Number,dy:Number,dz:Number):void{
			var i:Vect3d = basis.i, j:Vect3d = basis.j, k:Vect3d = basis.k;
			position.x += dx*i.x + dy*j.x + dz*k.x;
			position.y += dx*i.y + dy*j.y + dz*k.y;
			position.z += dx*i.z + dy*j.z + dz*k.z;
		}
		
		// UNUSED
			// rotate about _up axis thru point p by angle
			public function spinAbout(p:Vert3d,angle:Number){
				var rel:Vert3d = VertexMaths.sub(position,p);
				var m:Matrix4 = MatrixMaths.axisRot(_up,angle);
				MatrixMaths.vectMatMult3x3(m,rel);
				position = p.clone();
				VertexMaths.addTo(position,rel);
			}
		
		public function get sz():Number{return _sz;}
		public function set sz(s:Number):void{_sz = s;}
	}
}